package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FastDFSUtil {
    /**
     * 加载tracker连接信息
     */
    static {
        try {
//            查找classpath下的文件路径
            String filename = new ClassPathResource("fdfs_client.conf").getPath();
//            加载tracker连接信息
            ClientGlobal.init(filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param fastDFSFile 上传文件信息封装
     * @return
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception {
//        附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", fastDFSFile.getAuthor());
//      获取trackerServer
        TrackerServer trackerServer = getTrackerServer();
//      获取StorageServer
        StorageClient storageClient = getStorageClient(trackerServer);
        /**
         * 通过StorageClient访问Storage，实现文件上传，并获取文件上传后的存储信息
         * 1.上传文件的字节数组
         * 2.文件的扩展名 jpg
         * 3.附加参数 比如： 拍摄地址：北京
         * uploads[]
         *      uploads[0] :文件上传所存储的 Storage 的组名字 group1
         *      uploads[1] :文件存储到storage上的文件名字  M00/02/44/itheima.jpg
         */
        String[] uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);
        return uploads;
    }

    /**
     * 获取文件信息
     *
     * @param groupName      文件组的名字
     * @param remoteFileName 文件的存储路径名字，
     * @return
     */
    public static FileInfo getFile(String groupName, String remoteFileName) throws Exception {
//      获取trackerServer
        TrackerServer trackerServer = getTrackerServer();
//      获取StorageServer
        StorageClient storageClient = getStorageClient(trackerServer);
//        获取文件信息
        return storageClient.get_file_info(groupName, remoteFileName);
    }

    /**
     * 获取文件信息
     *
     * @param groupName      文件组的名字
     * @param remoteFileName 文件的存储路径名字，
     * @return
     */
    public static InputStream downloadFile(String groupName, String remoteFileName) throws Exception {
//      获取trackerServer
        TrackerServer trackerServer = getTrackerServer();
//      获取StorageServer
        StorageClient storageClient = getStorageClient(trackerServer);
//        文件下载
        byte[] buffer = storageClient.download_file(groupName, remoteFileName);

        return new ByteArrayInputStream(buffer);
    }

    /**
     * 获取文件信息
     *
     * @param groupName      文件组的名字
     * @param remoteFileName 文件的存储路径名字，
     * @return
     */
    public static void deleteFile(String groupName, String remoteFileName) throws Exception {
//      获取trackerServer
        TrackerServer trackerServer = getTrackerServer();
//      获取StorageServer
        StorageClient storageClient = getStorageClient(trackerServer);
//        删除文件
        storageClient.delete_file(groupName, remoteFileName);
    }

    /**
     * 获取storage信息
     *
     * @return
     * @throws Exception
     */
    public static StorageServer getStorages() throws Exception {
//        创建一个TrackerClient对象，通过TrackerClient访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();
//        通过TrackerClient获取TrackerServer的连接对象
        TrackerServer trackerServer = trackerClient.getConnection();
//      获取storage信息
        return trackerClient.getStoreStorage(trackerServer);
    }

    /**
     * 获取storage的端口和IP信息
     *
     * @param groupName      文件组的名字
     * @param remoteFileName 文件的存储路径名字，
     * @return
     * @throws Exception
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws Exception {
//        创建一个TrackerClient对象，通过TrackerClient访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();
//        通过TrackerClient获取TrackerServer的连接对象
        TrackerServer trackerServer = trackerClient.getConnection();

//        获取storage的端口和IP信息
        return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
    }

    public static String getTrackerInfo() throws Exception {
//      获取trackerServer
        TrackerServer trackerServer = getTrackerServer();

//        tracker 的IP、HTTP端口
        String ip = trackerServer.getInetSocketAddress().getHostString();
        int tracker_http_port = ClientGlobal.getG_tracker_http_port();
        String url = "http://" + ip + ":" + tracker_http_port;
        return url;
    }

    public static TrackerServer getTrackerServer() throws Exception {
//        创建一个tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

//        通过trackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        return trackerServer;
    }

    public static StorageClient getStorageClient(TrackerServer trackerServer){
//        通过TrackerServer的连接信息可以获取Storage的连接信息，创建StorageClient对象存储Storage的连接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);
        return storageClient;
    }

    public static void main(String[] args) throws Exception {
        //获取文件信息
//        FileInfo group1 = getFile("group1", "M00/00/00/wKgAZmAvs9iAZf_9AAMEVBO42Do863.jpg");
//        System.out.println(group1);

////        文件下载
//        InputStream is = downloadFile("group1", "M00/00/00/wKjphGBFvPeAe41PAADgfiXhGYM756.jpg");
//        FileOutputStream os = new FileOutputStream("D:\\360downloads\\f1.jpg");
//
//        byte[] buffers = new byte[1024];
//        while (is.read(buffers)!=-1){
//            os.write(buffers);
//        }
//        os.flush();
//        os.close();
//        is.close();

//        删除文件
//        deleteFile("group1", "wKjphGBFvPeAe41PAADgfiXhGYM756.jpg");

//        获取storage信息
//        StorageServer storages = getStorages();
//        System.out.println(storages.getStorePathIndex());
//        System.out.println(storages.getInetSocketAddress().getHostString());

//        获取storage组的IP和端口信息
//        ServerInfo[] groups = getServerInfo("group1", "wKgAZmAvlM2AekIKAAMEVBO42Do933.jpg");
//        for (ServerInfo group : groups) {
//            System.out.println(group.getIpAddr());
//            System.out.println(group.getPort());
//        }

//        获取tracker信息
//        System.out.println(getTrackerInfo());
    }


}
